package com.hrbu.ssm.controller;

import com.hrbu.ssm.request.RespBody;
import com.hrbu.ssm.request.Result;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * @Description:
 * @Author: tanyajun
 * @CreateTime: 2021/3/26
 * @Blog: https://tanyajun.top
 */
@Controller
public class UserController {
    /**
     * 向用户登录页面跳转
     *
     * @return
     */
    @GetMapping("login")
    public String toLogin() {
        return "forward:login.jsp";
    }

    @PostMapping("login")
    @ResponseBody
    public Result toMain(HttpSession session, Model model, @RequestParam("account") String account, @RequestParam("password") String password) {
        if ("admin".equals(account) && "admin".equals(password)) {
            // 登陆成功存入session
            session.setAttribute("USER_NAME", "谭亚军");
            // 跳转主页面
            return Result.ok(1);
        }
        model.addAttribute("msg", "用户名或密码错误");
        return Result.error(0).setMessage("用户名或密码错误");
    }

    @GetMapping("main")
    public String toMain() {
        return "main";
    }

    @GetMapping("logout")
    public String logout(HttpSession session) {
        // 退出登录清除session
        session.invalidate();
        return "redirect:login";
    }
}
